﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainUI
{
    public class RDWWrapperUI
    {
        private static RDWWrapperUI wrapper = new RDWWrapperUI(new MainForm());
        public MainForm FormMain;

        public static RDWWrapperUI Wrapper
        {
            get { return wrapper; }
        }
        public RDWWrapperUI(MainForm _FormMain)
        {
            FormMain = _FormMain;
            FormMain.Load += FormMain_Load;
            FormMain.FormClosing += FormMain_FormClosing;
        }

        void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {

            SaveMostRecentFiles(FormMain.openControl1.MRUFileList, Application.StartupPath + "\\" + MRUArrayList.MRUFileName);
            SaveMostRecentFiles(FormMain.saveAsControl1.MRUFolderList, Application.StartupPath + "\\" + MRUArrayList.MRUFolderName);
        }

        void FormMain_Load(object sender, EventArgs e)
        {
            InitMostRecentFiles(FormMain.openControl1.MRUFileList);
        }
        void InitMostRecentFiles(MRUArrayList arList)
        {
            string fileName = Application.StartupPath + "\\" + MRUArrayList.MRUFileName;
            string folderName = Application.StartupPath + "\\" + MRUArrayList.MRUFolderName;
            FormMain.openControl1.MRUFileList.Init(fileName, Application.StartupPath);
            FormMain.saveAsControl1.MRUFolderList.Init(folderName, Application.StartupPath);
        }
        private void AddToMostRecentFiles(string name, MRUArrayList arMRUList)
        {
            arMRUList.InsertElement(name);
        }
        private void AddToMostRecentFolders(string name, MRUArrayList arMRUList)
        {
            name = Path.GetFullPath(name);
            arMRUList.InsertElement(Path.GetDirectoryName(name));
        }
        void SaveMostRecentFiles(MRUArrayList arList, string fileName)
        {
            try
            {
                System.IO.StreamWriter sw = System.IO.File.CreateText(fileName);
                for (int i = 0; i < arList.Count; i++) sw.WriteLine(string.Format("{0},{1}", arList[i].ToString(), arList.GetLabelChecked(arList[i].ToString())));
                sw.Close();
            }
            catch { }
        }

    }
}
